package com.mashiro.system.service.impl;

import com.mashiro.common.utils.DateUtils;
import com.mashiro.common.utils.SecurityUtils;
import com.mashiro.system.domain.MoeAcceptance;
import com.mashiro.system.domain.MoeAcceptanceInfo;
import com.mashiro.system.domain.MoeMessage;
import com.mashiro.system.domain.MoeProject;
import com.mashiro.system.domain.status.MoeAcceptanceStatus;
import com.mashiro.system.domain.status.MoeProjectStatus;
import com.mashiro.system.domain.status.MoeWorkOrderStatus;
import com.mashiro.system.domain.types.MoeMessageType;
import com.mashiro.system.mapper.MoeAcceptanceMapper;
import com.mashiro.system.mapper.MoeMessageMapper;
import com.mashiro.system.mapper.MoeProjectMapper;
import com.mashiro.system.service.IMoeAcceptanceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 验收管理Service业务层处理
 *
 * @author mashiro
 * @date 2023-04-14
 */
@Service
public class MoeAcceptanceServiceImpl implements IMoeAcceptanceService {
    @Resource
    private MoeAcceptanceMapper moeAcceptanceMapper;

    @Resource
    private MoeProjectMapper moeProjectMapper;
    @Resource
    private MoeMessageMapper moeMessageMapper;

    /**
     * 查询验收管理
     *
     * @param id 验收管理主键
     * @return 验收管理
     */
    @Override
    public MoeAcceptance selectMoeAcceptanceById(Long id) {
        return moeAcceptanceMapper.selectMoeAcceptanceById(id);
    }

    /**
     * 查询验收管理列表
     *
     * @param moeAcceptance 验收管理
     * @return 验收管理
     */
    @Override
    public List<MoeAcceptance> selectMoeAcceptanceList(MoeAcceptance moeAcceptance) {
        return moeAcceptanceMapper.selectMoeAcceptanceList(moeAcceptance);
    }

    /**
     * 新增验收管理
     *
     * @param moeAcceptance 验收管理
     * @return 结果
     */
    @Override
    public int insertMoeAcceptance(MoeAcceptance moeAcceptance) {
        moeAcceptance.setCreateTime(DateUtils.getNowDate());
        return moeAcceptanceMapper.insertMoeAcceptance(moeAcceptance);
    }

    /**
     * 修改验收管理
     *
     * @param moeAcceptance 验收管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateMoeAcceptance(MoeAcceptance moeAcceptance) {
        MoeMessage message = new MoeMessage();
        Date date = DateUtils.getNowDate();
        moeAcceptance.setCreateTime(date);
        Long userId = SecurityUtils.getUserId();// 发送者
        MoeProject moeProject = moeProjectMapper.selectMoeProjectById(moeAcceptance.getProjectId());
        String title = moeProject.getName() + "的验收结果";// 标题
        message.setSenderId(userId);
        message.setTitle(title);
        message.setType(MoeMessageType.APPROVAL.getValue());// 审批通知
        if (MoeAcceptanceStatus.ACCEPTED.getValue().equals(moeAcceptance.getStatus())) {// 验收项目
            moeProject.setFinishTime(date);
            moeAcceptance.setAcceptanceTime(date);
            moeProject.setStatus(MoeWorkOrderStatus.APPROVED.getValue());// 更新项目状态为 已验收
            message.setContent("该项目完成得很好，辛苦大家了，审批意见: " + moeAcceptance.getAcceptanceOpinions());
        } else if (MoeAcceptanceStatus.REJECTED.getValue().equals(moeAcceptance.getStatus())) {// 驳回项目
            moeProject.setStatus(MoeWorkOrderStatus.REJECTED.getValue());// 更新项目状态为 已审批、未通过
            message.setContent("该项目仍存在不足，请继续完善，驳回意见: " + moeAcceptance.getAcceptanceOpinions());
        }
        message.setReceiverId(moeProject.getManagerId());// 配送消息给项目经理
        moeMessageMapper.insertMoeMessage(message);
        moeProjectMapper.updateMoeProject(moeProject);
        return moeAcceptanceMapper.updateMoeAcceptance(moeAcceptance);
    }

    /**
     * 批量删除验收管理
     *
     * @param ids 需要删除的验收管理主键
     * @return 结果
     */
    @Override
    public int deleteMoeAcceptanceByIds(Long[] ids) {
        return moeAcceptanceMapper.deleteMoeAcceptanceByIds(ids);
    }

    /**
     * 删除验收管理信息
     *
     * @param id 验收管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMoeAcceptanceById(Long id) {
        MoeAcceptance moeAcceptance = moeAcceptanceMapper.selectMoeAcceptanceById(id);
        Long projectId = moeAcceptance.getProjectId();
        MoeProject moeProject = new MoeProject();
        moeProject.setId(projectId);
        moeProject.setStatus(MoeProjectStatus.FINISHED.getValue());
        moeProjectMapper.updateMoeProject(moeProject);
        return moeAcceptanceMapper.deleteMoeAcceptanceById(id);
    }

    @Override
    public MoeAcceptanceInfo getAcceptanceInfo(Long id) {
        return moeAcceptanceMapper.selectAcceptanceInfo(id);
    }
}
